package com.djmw.mouse.web.hr;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import javax.validation.Validator;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.djmw.mouse.entity.Company;
import com.djmw.mouse.entity.HrUser;
import com.djmw.mouse.entity.Industry;
import com.djmw.mouse.service.CompanyService;
import com.djmw.mouse.service.IndustryService;
import com.djmw.mouse.service.account.HrAccountService;
import com.djmw.mouse.service.account.ShiroDbRealm.ShiroUser;
import com.djmw.mouse.service.constants.Constants;

/**
 * 公司信息维护的Controller, 使用Restful风格的Urls:
 * 
 * View & Update page : GET /hr/option/company <br>
 * Update action : POST /hr/option/company <br>
 * 
 * @author michael
 */

@Controller
@RequestMapping(value = "/hr/option/company-profile")
public class CompanyInfoController {

	@Autowired
	private CompanyService companyService;
	@Autowired
	private HrAccountService hrAccountService;
	@Autowired
	private IndustryService industryService;
	@Autowired
	private Validator validator;

	@RequestMapping(value = "")
	public String view(Model model, ServletRequest request) {
		HrUser user = getCurrentUser();
		Company company = user.getCompany();
		model.addAttribute("company", company);
		model.addAttribute("companyTypes", Constants.COMPANY_TYPES);
		model.addAttribute("companySizes", Constants.COMPANY_SIZES);
		model.addAttribute("companyProvinces", Constants.PROVINCES);
		model.addAttribute("companyCities", Constants.CITIES);
		return "hr/option/company-profile";
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadCompany") Company company, Long industryId, Long industry2Id,
			RedirectAttributes redirectAttributes) {
		Industry industry;
		if (industryId != null) {
			if ((industry = industryService.getIndustry(industryId)) != null) {
				company.setIndustry(industry);
			}
		} else {
			company.setIndustry(null);
		}
		if (industry2Id != null) {
			if ((industry = industryService.getIndustry(industry2Id)) != null) {
				company.setIndustry2(industry);
			}
		} else {
			company.setIndustry2(null);
		}
		companyService.saveCompany(company);
		redirectAttributes.addFlashAttribute("message", "更新公司信息成功");
		return "redirect:/hr/option/company-profile/";
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出Company对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadCompany")
	public Company getCompany(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return companyService.getCompany(id);
		}
		return null;
	}

	/**
	 * 取出Shiro中的当前用户.
	 */
	private HrUser getCurrentUser() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return (HrUser) user.getUser();
	}
}
